package com.xiaoy.servlet;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xiaoy.util.format.FileUtil;


public class MsgServlet extends BaseServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1091625455313315279L;

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		
		try {
//			Writer writer = resp.getWriter();
//			String echostr = request.getParameter("echostr");
//			if (echostr!=null&&!"".equals(echostr)) {
//				writer.write(echostr);
//				return ;
//			}
		System.out.println("i am coming...");
			InputStream is = request.getInputStream();
//			String content = 
//					"<xml> "
//					+ "<URL><![CDATA[http://0.chentianyi.duapp.com/msg]]></URL> "
//					+ "<ToUserName> <![CDATA[chenty1989]]> </ToUserName>"
//					+ "<FromUserName> <![CDATA[OpenID]]> </FromUserName> "
//					+ "<CreateTime>2013</CreateTime> "
//					+ "<MsgType> <![CDATA[text]]> </MsgType> "
//					+ "<Content> <![CDATA[test1]]> </Content> "
//					+ "<MsgId>1</MsgId> " 
//					+ "</xml>";
//			
//			InputStream is = new ByteArrayInputStream(content.getBytes());
			
			byte[] buffer = new byte[4096];
//
			StringBuffer out = new StringBuffer();
			for (int n; (n = is.read(buffer)) != -1;) {
				out.append(new String(buffer, 0, n));
			}
//			
//			Document document = new SAXReader().read(is);
//			Element root = document.getRootElement();
//			List<Element> childElements = root.elements();
//			Map<String,Object> map = new HashMap<String, Object>();
//			for (Element child : childElements) {
//				System.out.println(child.getName() + ": " + child.getText());
//				map.put(child.getName(), child.getText());
//			}
			
//			String rep = 
//					"<xml>"+
//						"<ToUserName><![CDATA[$ToUserName]]></ToUserName>"+
//						"<FromUserName><![CDATA[$FromUserName]]></FromUserName>"+
//						"<CreateTime>12345678</CreateTime>"+
//						"<MsgType><![CDATA[1]]></MsgType>"+
//						"<Content><![CDATA[你好]]></Content>"+
//					"</xml>";
			System.out.println("sb-->"+out.toString());
			
//			rep = rep.replace("$ToUserName", map.get("FromUserName").toString())
//					.replace("$FromUserName", map.get("ToUserName").toString());
//			System.out.println("rep-->"+rep);
//			resp.getWriter().write(rep);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
//		request.setAttribute("msg", out.toString());
//		request.getRequestDispatcher("index.jsp").forward(request, resp);
	}

	
	
	public static void main(String[] args) throws ServletException, IOException {
		
//		System.out.println("sssss");
//		String content = 
//				"<xml> "
//				+ "<URL><![CDATA[http://0.chentianyi.duapp.com/msg]]></URL> "
//				+ "<ToUserName> <![CDATA[chenty1989]]> </ToUserName>"
//				+ "<FromUserName> <![CDATA[OpenID]]> </FromUserName> "
//				+ "<CreateTime>2013</CreateTime> "
//				+ "<MsgType> <![CDATA[text]]> </MsgType> "
//				+ "<Content> <![CDATA[test1]]> </Content> "
//				+ "<MsgId>1</MsgId> " 
//				+ "</xml>";
		
//		content = "<xml><b> "
//				+ "<URL>http://0.chentianyi.duapp.com/msg</URL> "
//				+ "<ToUserName>chenty1989</ToUserName>"
//				+ "<FromUserName>OpenID</FromUserName> "
//				+ "<CreateTime>2013</CreateTime> "
//				+ "<MsgType>text</MsgType> "
//				+ "<content><msge>test1</msge></content> "
//				+ "<msgid>1</msgid> " 
//				+ "</b></xml>";
				
//		content = "<books> " 
//				+ "  <book id=\"001\">  "
//				+ "     <title>Harry Potter</title>  "
//				+ "     <author>J K. Rowling</author>  " 
//				+ "  </book>  "
//				+ "  <book id=\"002\">  "
//				+ "     <title>Learning XML</title>  "
//				+ "     <author>Erik T. Ray</author>  " 
//				+ "  </book>  "
//				+ "</books> ";
//		InputStream is;
//		try {
//			
//			is = new ByteArrayInputStream(content.getBytes());
//			
//			SAXReader reader = new SAXReader();
//			Document document = reader.read(is);
//			Element root = document.getRootElement();
//			
//			List<Element> childElements = root.elements();
//			
//			for (Element child : childElements) {
//				
//				System.out.println(child.getName() + ": " + child.getText());
//				
//				List<Element> elementList = child.elements();
//				for (Element ele : elementList) {
//					System.out.println(ele.getName() + ": " + ele.getText());
//				}
////				System.out.println();
//				
//				List<Attribute> attributeList = child.attributes();
//				for (Attribute attr : attributeList) {
//					System.out.println(attr.getName() + ": " + attr.getValue());
//				}
////				System.out.println();
//			}
//			System.out.println("ddddddddddddd");
//			
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
	
//	{
//		byte[] buffer = new byte[4096];
//
//		StringBuffer out = new StringBuffer();
//		for (int n; (n = is.read(buffer)) != -1;) {
//			out.append(new String(buffer, 0, n));
//		}
//		System.out.println(out);
//	writer.write(out.toString());
		
		
		
		new MsgServlet().doPost(null, null);
	}
	
}
